// 测试 matchPoolCron 的脚本
// 在 Back4App Dashboard → Console 中运行

// 1. 添加测试玩家到匹配池
async function addTestPlayers() {
    const players = [
        { difficulty: 1, username: "玩家A" },
        { difficulty: 1, username: "玩家B" },
        { difficulty: 2, username: "玩家C" },
        { difficulty: 2, username: "玩家D" },
        { difficulty: 2, username: "玩家E" },
        { difficulty: 3, username: "玩家F" },
        { difficulty: 3, username: "玩家G" }
    ];
    
    for (const player of players) {
        try {
            const result = await Parse.Cloud.run("joinMatchPool", player);
            console.log(`Added ${player.username}:`, result);
        } catch (error) {
            console.error(`Error adding ${player.username}:`, error);
        }
    }
}

// 2. 手动触发匹配池处理
async function testMatchPool() {
    try {
        const result = await Parse.Cloud.run("testMatchPoolProcessing");
        console.log("Match pool test result:", result);
    } catch (error) {
        console.error("Match pool test error:", error);
    }
}

// 3. 获取匹配池统计
async function getStats() {
    try {
        const result = await Parse.Cloud.run("getMatchPoolStats");
        console.log("Match pool stats:", result);
    } catch (error) {
        console.error("Stats error:", error);
    }
}

// 4. 清理测试数据
async function cleanupTestData() {
    try {
        // 清理匹配池
        const MatchPool = Parse.Object.extend("MatchPool");
        const query = new Parse.Query(MatchPool);
        const results = await query.find();
        await Parse.Object.destroyAll(results);
        console.log(`Cleaned up ${results.length} match pool entries`);
        
        // 清理测试房间
        const Room = Parse.Object.extend("Room");
        const roomQuery = new Parse.Query(Room);
        const rooms = await roomQuery.find();
        await Parse.Object.destroyAll(rooms);
        console.log(`Cleaned up ${rooms.length} rooms`);
        
    } catch (error) {
        console.error("Cleanup error:", error);
    }
}

// 运行测试
console.log("=== 开始测试 matchPoolCron ===");

// 步骤1: 添加测试玩家
console.log("\n1. 添加测试玩家...");
await addTestPlayers();

// 等待2秒让定时任务运行
console.log("\n2. 等待2秒让定时任务运行...");
await new Promise(resolve => setTimeout(resolve, 2000));

// 步骤2: 检查统计
console.log("\n3. 检查匹配池统计...");
await getStats();

// 步骤3: 手动测试
console.log("\n4. 手动测试匹配处理...");
await testMatchPool();

// 步骤4: 再次检查统计
console.log("\n5. 再次检查统计...");
await getStats();

console.log("\n=== 测试完成 ===");

// 清理选项 (取消注释以运行)
// await cleanupTestData();